import { UndoTextOperation, Operation } from "../operations";
import Editor from "../editor";
import { debounce } from '../utils'

export function UndoRedoPlugin (editor: Editor): Editor {
    const undo = function (this: Editor) {
        const undoTextOperation = new UndoTextOperation()
        this.apply(undoTextOperation)
    }
    editor.undo = undo.bind(editor)
    // apply 方法重写
    let operationList: Operation[] = []
    const addHistory = debounce((operation: Operation[]) => {
        editor.undoModel.addStep(operation)
        operationList = []
    }, 300)
    const apply = editor.apply.bind(editor)
    editor.apply = (operation: Operation) => {
        apply(operation)
        if (operation instanceof UndoTextOperation) {
           
        } else {
            
            operationList.push(operation)
            addHistory(operationList)
        }
    }

    return editor
}